const router = require('koa-router')()
const CreateSequelize = require('./sequelize')
let sequelize = new CreateSequelize()
sequelize.init_sequeleze()

router.get('/OneUser', async (ctx, next) => {
  console.log(ctx.query);
  let res = await sequelize.select('select * from auser')
  ctx.body = res
})
router.get('/setUser', async (ctx, next) => {
  let { name, user, pass, state, tiem } = ctx.query
  console.log(name, user, pass, state, tiem);
  await sequelize.select(`insert into auser(name,state,user,pass,tiem) values('${name}','${state === '管理员' ? 1 : 2}','${user}','${pass}','${tiem}')`)
  ctx.body = '连接成功'
})
router.get('/delUser', async (ctx, next) => {
  console.log(ctx.query);
  let { index } = ctx.query
  await sequelize.select(`delete from auser where ifID='${index}'`)
  ctx.body = '连接成功'
})
router.get('/editOk', async (ctx, next) => {
  console.log(ctx.query);
  let { name, user, pass, state, ifID } = ctx.query
  await sequelize.select(`update auser set name='${name}',user='${user}',pass='${pass}',state='${state === '管理员' ? 1 : 2}' where ifID='${ifID}'`)
  ctx.body = '连接成功'
})
router.get('/forgetPass', async (ctx, next) => {
  console.log(ctx.query);
  let { pass, ifID } = ctx.query
  await sequelize.select(`update auser set pass='${pass}' where ifID='${ifID}'`)
  ctx.body = '连接成功'
  let res = await sequelize.select('select * from auser')
  ctx.body = res
})


// ---------------------------------------------------

// 首页接口和订单列表详情
// 获取订单数据
router.get("/getOrderList", async (ctx) => {
  let res = await sequelize.select(`select * from orderlist`)
  ctx.body = {
    msg: "获取订单数据",
    data: res
  }
})

//==========================
router.get('/selectList', async (ctx, next) => {
  let res = await sequelize.select('select * from orderlist1')
  console.log(res);
  ctx.body = res
})
router.get('/sortList', async (ctx, next) => {
  let { order, prop } = JSON.parse(ctx.query.res)
  console.log(prop);
  let res = await sequelize.select('select * from orderlis')

  if (order === 'ascending') {
    console.log(order);
    let arr = res.sort((a, b) => {
      return b.ordermoney - a.ordermoney
    })
    ctx.body = arr
  }
  if (prop === 'ordermoney') {
    console.log(prop);
    let arr = res.sort((a, b) => {
      return a.ordermoney - b.ordermoney
    })
    ctx.body = arr
  }

})
router.get('/filterAll', async (ctx, next) => {
  let { time, iphone, selectdp, selectqis } = JSON.parse(ctx.query.res)
  console.log(selectdp);
  let res = await sequelize.select('select * from orderlist1')
  let arr = res.filter((item) => {
    if (selectdp === '全部' && selectqis === '全部') {
      return item.downTime.indexOf(time) > -1 &&
        item.phoneNum.indexOf(iphone) > -1
    } else if (selectdp !== '全部' && selectqis === '全部') {
      return item.downTime.indexOf(time) > -1 &&
        item.phoneNum.indexOf(iphone) > -1 &&
        item.star.indexOf(selectdp) > -1 ||
        item.kni.indexOf(selectqis) > -1
    } else if (selectdp === '全部' && selectqis !== '全部') {
      return item.downTime.indexOf(time) > -1 &&
        item.phoneNum.indexOf(iphone) > -1 &&
        item.star.indexOf(selectdp) > -1 ||
        item.kni.indexOf(selectqis) > -1
    } else if (selectdp !== '全部' && selectqis !== '全部') {
      return item.downTime.indexOf(time) > -1 &&
        item.phoneNum.indexOf(iphone) > -1 &&
        item.star.indexOf(selectdp) > -1 &&
        item.kni.indexOf(selectqis) > -1
    } else {
      return item.downTime.indexOf(time) > -1 &&
        item.phoneNum.indexOf(iphone) > -1 &&
        item.star.indexOf(selectdp) > -1 &&
        item.kni.indexOf(selectqis) > -1
    }
  })

  ctx.body = arr
})


//-----------------------------------


//商品管理
router.get('/GetGdata', async (ctx, next) => {
  // console.log(JSON.parse(ctx.query.data));
  let { fselop, sselop, tselop } = JSON.parse(ctx.query.data);
  console.log(fselop, sselop, tselop);
  if (tselop === "-1") {
    if (sselop === "-1") {
      var data = await sequelize.select(`select * from data where name like '%${fselop}%' `);
    } else {
      var data = await sequelize.select(`select * from data where name like '%${fselop}%' and \`Index\`='${sselop}' `);
    }
  } else {
    if (sselop === "-1") {
      var data = await sequelize.select(`select * from data where name like '%${fselop}%' and state='${tselop}' `);
    } else {
      var data = await sequelize.select(`select * from data where name like '%${fselop}%' and \`Index\`='${sselop}' and state='${tselop}' `);
    }
  }
  ctx.body = data
})
//商品上下架修改
router.get('/cgoodsstate', async (ctx, next) => {
  let { gid, gstate } = JSON.parse(ctx.query.data)
  if (gstate === "1") {
    await sequelize.select(`update data set state='3' where id='${gid}'`)
  } else {
    await sequelize.select(`update data set state='1' where id='${gid}'`)
  }
  ctx.body = { code: "01", msg: "修改成功" }
})
//商品删除
router.get('/goodsdel', async (ctx, next) => {
  let { gid } = JSON.parse(ctx.query.data)
  await sequelize.select(`delete from data where id='${gid}'`)
  ctx.body = { code: "01", msg: "删除成功" }
})
//商品添加
router.get('/addgoods', async (ctx, next) => {
  let { obj } = JSON.parse(ctx.query.data)
  await sequelize.select(`insert into data (name,img,\`Index\`,state) values ('${obj.name}','${obj.img}','${obj.Index}','${obj.state}')`)
  ctx.body = { code: "01" }
})
//商品修改
router.get('/goodschange', async (ctx, next) => {
  let { gid, obj } = JSON.parse(ctx.query.data)
  console.log(gid, obj);
  await sequelize.select(`update data set name='${obj.name}',img='${obj.img}',\`Index\`='${obj.Index}',state='${obj.state}' where id='${gid}'`)
  ctx.body = { code: "01" }
})
//商品排序
router.get('/orderdata', async (ctx, next) => {
  let { orderstr } = JSON.parse(ctx.query.data);
  if (orderstr) {
    var data = await sequelize.select(`select * from data order by ${orderstr}`);
    // console.log(`select * from data order by ${orderstr}`);
  } else {
    var data = await sequelize.select(`select * from data`);
  }
  ctx.body =data
})
module.exports = router